Hello,
I'm having a model with a uniqueness validator, an action, and a view with a form, and everything is working fine, records get added etc, but whenever i try to add something to the database with $this->create() that already exists, or anything that makes the query fail, it's breaking the entire code leaving no error message.
When i wrap the following try/catch around $user->create(); in my controller
try {
$user->create();
} catch (\Exception $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
echo " File=", $e->getFile(), "\n";
echo " Line=", $e->getLine(), "\n";
echo $e->getTraceAsString();
}
..and add a record that already exists, i'm getting
Phalcon\Mvc\Model\ValidationFailed: Value of field 'username' is already present in another record
Which is fine and expected.
However, without the try/catch, if i run the following code, neither "failed", nor "failed 2" are displayed. The code just breaks and nothing after the if() condition is executed, not even the echo 'failed 2';, leaving the action and the view form broken.
if($user->create())
{
echo 'success'; // not executed
}
else
{
echo 'failed'; // not executed
}
echo 'failed 2'; // not executed
here is my model and the action in my controller:
use Phalcon\Mvc\Model\Validator\Uniqueness;
class Users extends Phalcon\Mvc\Model
{
public function validation()
{
$this->validate(new Uniqueness(array(
'field' => 'email',
"message" => "Value of field 'email' is already present in another record"
)));
$this->validate(new Uniqueness(array(
'field' => 'username',
"message" => "Value of field 'username' is already present in another record"
)));
if ($this->validationHasFailed() == true) {
return false;
}
}
public function initialize() {
/**
* Skip 'status' on insertion as it is a default value
*/
$this->skipAttributesOnCreate(array('status'));
}
}
public function registerAction() {
$form = new RegisterForm();
if ($this->request->isPost()) {
if ($this->security->checkToken()) {
if ($form->isValid($_POST)) {
$user = new Users;
$user->username = $this->request->getPost('username');
$user->email = $this->request->getPost('email');
$user->password = $this->security->hash($this->request->getPost('password'));
$user->created_at = time();
if($user->create())
{
echo 'success';
}
else
{
echo 'failed'; // not executed if $user->create() fails
}
echo 'failed 2'; // not executed if $user->create() fails
}
else
{
echo $form->getMessages()[0];
}
}
}
$this->view->form = $form; // not executed if $user->create() fails (= breaks registration form)
}
As you see there is also a $this->view->form = $form; at the end of the action - when the create() fails, this is neither executed, leaving the view broken with the following error message:
Fatal error: Call to a member function getLabel() on a non-object in C:\wamp\www\test.local\app\views\session\register.phtml on line 11
Is there anything i'm doing wrong? I don't really know what to do and i'm stuck with this for a while now
(i have created a topic about this two days ago but i failed at explaining my problem better and i don't know what to do and need to solve this quick)
Thanks for any replies